﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharePointBrowser.WebsWS;
using System.Xml;
using System.Net;
using SharePointBrowser.Data;
using SharePointBrowser.Forms;

namespace SharePointBrowser
{
    public partial class MainForm : Form
    {
        enum ActiveControl { Web, List };

        private WebList websControl;
        private ListList listsControl;
        private ActiveControl activeControl;

        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void miOpenSite_Click(object sender, EventArgs e)
        {
            if (websControl != null)
            {
                Controls.Remove(websControl);
                websControl.Dispose();
                websControl = null;
            }
        }

        private void btnOpenSite_Click(object sender, EventArgs e)
        {
            SPAdapter.Instance.SiteUrl = txtSiteUrl.Text;
            string[] domainUsername = txtUsername.Text.Split('\\');
            SPAdapter.Instance.Credentials = domainUsername.Length > 1 
                ? new NetworkCredential(domainUsername[1], txtPassword.Text, domainUsername[0]) 
                : new NetworkCredential(domainUsername[0], txtPassword.Text);

            if (websControl == null)
            {
                websControl = new WebList();
            }
            websControl.SiteUrl = txtSiteUrl.Text;
            websControl.Dock = DockStyle.Fill;
            Controls.Add(websControl);
            websControl.BringToFront();
            websControl.LoadData();

            websControl.RootSiteReached += (s, ea) => miBack.Enabled = false;
            websControl.SubsiteOpened += (s, ea) => miBack.Enabled = true;
            websControl.ListOpened += new EventHandler<ListEventArgs>(websControl_ListOpened);

            activeControl = ActiveControl.Web;

            miNewItem.Enabled = false;
            miEditItem.Enabled = false;
            miViewItem.Enabled = false;
        }

        void websControl_ListOpened(object sender, ListEventArgs e)
        {
            if (listsControl == null)
            {
                listsControl = new ListList();
                listsControl.Dock = DockStyle.Fill;
                Controls.Add(listsControl);
            }
            listsControl.ListId = e.ListId;
            listsControl.SiteUrl = ((WebList)sender).SiteUrl;
            listsControl.ListName = e.Name;
            listsControl.LoadData();
            listsControl.BringToFront();
            activeControl = ActiveControl.List;
            miBack.Enabled = true;

            miNewItem.Enabled = true;
            miEditItem.Enabled = true;
            miViewItem.Enabled = true;
        }

        private void miSettings_Click(object sender, EventArgs e)
        {
            SettingsForm settingsForm = new SettingsForm();
            settingsForm.ShowDialog();
        }

        private void miBack_Click(object sender, EventArgs e)
        {
            if (activeControl == ActiveControl.Web)
            {
                websControl.GoBack();
            }
            else
            {
                if (listsControl.AtRootFolder)
                {
                    websControl.BringToFront();
                    activeControl = ActiveControl.Web;
                    miBack.Enabled = !websControl.AtRootSite;
                    miNewItem.Enabled = false;
                    miEditItem.Enabled = false;
                    miViewItem.Enabled = false;
                }
                else
                {
                    listsControl.GoBack();
                }
            }
        }

        private void MainForm_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void miNewItem_Click(object sender, EventArgs e)
        {
            NewItem newItem = new NewItem()
            {
                SiteUrl = listsControl.SiteUrl,
                ListId = listsControl.ListId,
                ListName = listsControl.ListName
            };
            if (newItem.ShowDialog() == DialogResult.OK)
            {
                listsControl.LoadData();
            }
        }

        private void miViewItem_Click(object sender, EventArgs e)
        {

        }

        private void miEditItem_Click(object sender, EventArgs e)
        {

        }
    }
}